// userDatabase.js
const bcrypt = require('bcrypt');

// 模拟用户数据库
const users = [];

// 注册用户
async function registerUser(userData) {
    const { username, password } = userData;
    const existingUser = users.find(user => user.username === username);
    if (existingUser) {
        return null;
    }
    const hashedPassword = await bcrypt.hash(password, 10);
    const newUser = {
        ...userData,
        passwordHash: hashedPassword
    };
    users.push(newUser);
    return newUser;
}

// 验证用户登录
async function verifyUserLogin(username, password) {
    const user = users.find(u => u.username === username);
    if (!user) {
        return null;
    }
    const isPasswordValid = await bcrypt.compare(password, user.passwordHash);
    if (!isPasswordValid) {
        return null;
    }
    return user;
}

module.exports = {
    registerUser,
    verifyUserLogin
};